// 重写数组的部分方法
let oldArrayProto = Array.prototype

// newArrayProto.proto=oldArrayProto
export let newArrayProto = Object.create(oldArrayProto)

let methods = [
    'push',
    'pop',
    'shift',
    'unshift',
    'reverse',
    'sort',
    'splice'
]

methods.forEach(method => {
    newArrayProto[method] = function (...args) {
        const result = oldArrayProto[method].call(this, ...args)// 老方法 函数的劫持 切片
        // console.log('method', method)

        //对新增的做处理，再劫持
        let inserted
        let ob = this.__ob__
        switch (method) {
            case 'push':
            case 'unshift':
                inserted = args
                break;
            case 'splice':
                inserted = args.slice(2)//从第3个开始
                break;

            default:
                break;
        }
        // console.log(inserted)
        if (inserted) {//对新增的内容再次进行观测
            ob.observeArray(inserted)
        }
        console.log('更新')
        ob.dep.notify()//数组变化了，通知对应的watcher实现更新逻辑
        return result
    }
})
